/******************************************************************************
 * @File name   :      ActivationServiceImpl.java
 *
 * @Author      :      ANMA
 *
 * @Date        :      Jul 31, 2015
 *
 * @Copyright Notice: 
 * Copyright (c) 2014 DMS, Inc. All  Rights Reserved.
 * This software is published under the terms of the DMS Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * ----------------------------------------------------------------------------
 * Date                         Who         Version         Comments
 * Jul 31, 2015 2:41:00 PM    ANMA      1.0            Initial Version
 *****************************************************************************/
package com.cap.bts.crmdp.dealer.mars.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cap.bts.commonfunc.common.constant.SmsTemplateNoConstants;
import com.cap.bts.commonfunc.common.constant.SmsTemplateVariable;
import com.cap.bts.commonfunc.common.dto.SmsContentDetailDto;
import com.cap.bts.commonfunc.common.util.UserUtil;
import com.cap.bts.crmdp.dealer.activation.dao.IPackageRedemptionLinesDao;
import com.cap.bts.crmdp.dealer.activation.dao.IPackageRedemptionLinesItemDao;
import com.cap.bts.crmdp.dealer.activation.domain.PackageRedemptionLines;
import com.cap.bts.crmdp.dealer.activation.dto.ActivationQueryDto;
import com.cap.bts.crmdp.dealer.activation.dto.ActivationResultsDto;
import com.cap.bts.crmdp.dealer.coupon.constants.CouponConstants;
import com.cap.bts.crmdp.dealer.coupon.dao.ITicketLineDefineDao;
import com.cap.bts.crmdp.dealer.coupon.dao.ITicketPriceOfDealerDao;
import com.cap.bts.crmdp.dealer.coupon.domain.Ticket;
import com.cap.bts.crmdp.dealer.mars.service.IMarsActivationService;
import com.cap.bts.crmdp.dealer.mars.service.IMarsMemberGiftService;
import com.cap.bts.crmdp.dealer.member.dao.IMaintainCarPlateDao;
import com.cap.bts.crmdp.dealer.member.dao.IMaintainMemberDao;
import com.cap.bts.crmdp.dealer.member.domain.CarPlateInfo;
import com.cap.bts.crmdp.dealer.member.domain.MemberInfo;
import com.cap.bts.crmdp.dealer.member.service.IMaintainCarPlateService;
import com.cap.bts.crmdp.dealer.purchase.constants.PurchaseConstants;
import com.cap.bts.crmdp.dealer.purchase.dao.IMemberPackageDao;
import com.cap.bts.crmdp.dealer.purchase.dao.IPackageDefinitionDao;
import com.cap.bts.crmdp.dealer.purchase.domain.MemberPackageInfo;
import com.cap.bts.crmdp.dealer.purchase.domain.PackageDefinition;
import com.cap.bts.crmdp.dealer.purchase.dto.MemberPackageInfoSubmitDto;
import com.cap.bts.crmdp.dealer.purchase.service.IPurchasePackageService;
import com.cap.bts.crmdp.dealer.redemption.dao.IPackageRedemptionDao;
import com.cap.bts.crmdp.dealer.redemption.domain.MemberPackageRedemption;
import com.cap.bts.crmdp.webservice.dto.CardDto;
import com.cap.bts.crmdp.webservice.dto.MarsResultDto;
import com.cap.bts.crmdp.webservice.dto.WSCrmActivationResultDto;
import com.cap.bts.crmdp.webservice.mars.client.CRM2MARS;
import com.cap.bts.framework.common.exception.AppException;
import com.cap.bts.framework.common.util.Utils;
import com.cap.bts.framework.sms.service.ISmContentService;
import com.cap.bts.framework.usermgnt.domain.User;

/**
 * 
 * 
 */
@Transactional(rollbackFor = AppException.class)
@Service
public class MarsActivationServiceImpl implements IMarsActivationService {

    private static final Logger LOGGER = Logger.getLogger(MarsActivationServiceImpl.class);

    private static final String ERR_MSG_NO_CARPLATE = "该会员没有绑定的车牌.";

    private static final String ERROR_CODE = "ERROR";
    private static final String SUCCESS_MSG = "successMsg";
    private static final String RESULT = "result";
    private static final String PACKAGE_ACTIVATION_MARS = "packageActivationMars";
    private static final String SUCCESS = "success";

    @Resource(name = "memberPackageDao")
    private IMemberPackageDao memberPackageDao;

    @Resource(name = "packageDefinitionDao")
    private IPackageDefinitionDao packageDefinitionDao;

    @Resource(name = "packageRedemptionLinesDao")
    IPackageRedemptionLinesDao packageRedemptionLinesDao;

    @Resource(name = "packageRedemptionDao")
    private IPackageRedemptionDao memberPackageRedemptionDao;

    @Autowired
    IMaintainCarPlateService maintainCarPlateService;

    @Resource(name = "maintainCarPlateDao")
    private IMaintainCarPlateDao maintainCarPlateDao;

    @Autowired
    IPurchasePackageService purchasePackageService;

    @Autowired
    IMarsMemberGiftService memberGiftService;

    @Autowired
    IMaintainMemberDao memberDao;

    @Autowired
    ISmContentService smContentService;

    @Autowired
    ITicketLineDefineDao iTicketLineDefineDao;

    @Autowired
    IPackageRedemptionLinesItemDao packageRedemptionLinesItemDao;
    
    @Autowired
    ITicketPriceOfDealerDao ticketPriceOfDealerDao;

    /*
     * (non-Javadoc)
     * 
     * @see com.cap.bts.crmdp.dealer.activation.service.IActivationService#
     * saveMemberActivation
     * (com.cap.bts.crmdp.dealer.activation.dto.ActivationQueryDto)
     */
    @Override
    public ActivationResultsDto saveMemberPurchaseAndActivationNowMars(
        MemberPackageInfoSubmitDto dto) throws AppException {
        //
        // 先购买再激活
        //
        ActivationQueryDto activationQueryDto = new ActivationQueryDto();
        if (dto.getPackageDefId() != null) {
            //
            // 购买
            //
            activationQueryDto.setPackageDefId(dto.getPackageDefId());
            dto.setIsPhysicalCard((byte) 1);
            purchasePackageService.purchasePackage(dto);
        }

        activationQueryDto.setDealerId(String.valueOf(dto.getDealerId()));
        activationQueryDto.setActiveMemberId(dto.getMemberId());
        activationQueryDto.setMobilePhone(dto.getMobilePhone());
        activationQueryDto.setMemberName(dto.getMemberName());
        activationQueryDto.setCarPlate(dto.getCarPlate());
        activationQueryDto.setPackageNumber(dto.getPackageNumber());
        activationQueryDto.setPackageActiveCode(dto.getPackageActiveCode());
        activationQueryDto.setPrice(dto.getPrice());
        activationQueryDto.setSoNumberActive(dto.getSoNumberActive());
        //
        // 激活
        //
        return this.saveMemberActivationMars(activationQueryDto);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.cap.bts.crmdp.dealer.activation.service.IActivationService#
     * saveMemberActivation
     * (com.cap.bts.crmdp.dealer.activation.dto.ActivationQueryDto)
     */
    @Override
    public ActivationResultsDto saveMemberActivationMars(ActivationQueryDto dto)
        throws AppException {

        ActivationResultsDto activationResultsDto = new ActivationResultsDto();
        Long activeMemberId = dto.getActiveMemberId();
        String carPlate = dto.getCarPlate();
        String errorMsg = "";
        // 依据车牌和会员ID找到车辆信息。
        List<CarPlateInfo> carPlateList = maintainCarPlateService
            .getCarPlateInfoListByCarPlateAndMemberId(carPlate, activeMemberId);
        LOGGER.info("active carPlate:" + carPlate);
        LOGGER.info("active activeMemberId:" + activeMemberId);
        if (carPlateList == null || carPlateList.isEmpty()) {
            throw new AppException(ERROR_CODE, ERR_MSG_NO_CARPLATE);
        }
        MemberPackageInfo cmpi = memberPackageDao.checkPackageNumberByActivationDto(dto);
        if (cmpi == null) {
            throw new AppException(ERROR_CODE, "卡号有误，请重新输入！");
        }
        if (cmpi.getPurchaseDate() == null) {
            throw new AppException(ERROR_CODE, "该卡号尚未售出，请核对您输入的卡号是否有误！");
        }
        if (cmpi.getMarsLockStatus() == 1) {
            throw new AppException(ERROR_CODE, "该卡号当前属于冻结状态（未进行结算），请先进行结算操作后再激活；如您已完成结算操作则请联系客服！");
        }
        MemberPackageInfo memberPackageInfo = memberPackageDao
            .getMemberPackageInfoByActivationDto(dto);
        if (memberPackageInfo != null) {
//            Long packageId = memberPackageInfo.getId();

            // PackageDefinition packageDefinition =
            // memberPackageInfo.getCardDefinition().getId()
            // Long pkgDefId = packageDefinition.getId()
            // 如果是当场激活时候，直接从画面内容中取得PackageDefId
            Long pkgDefId = dto.getPackageDefId();
            // 如果是事后激活，则从数据库中取得PackageDefId(PackageDefId在仅购买操作时保存提交到数据库)
            PackageDefinition pd = packageDefinitionDao.findById(memberPackageInfo
                .getPackageDefinition().getId());
            int validPeriod = pd.getValidPeriod();
            String unitOfPeriod = pd.getUnitOfPeriod();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());

            dto.setPackageDefId(pkgDefId);
            // 车牌ID
            Long carId = carPlateList.get(0).getId();
            Long maxPurchaseNum = packageDefinitionDao
                .findById(cmpi.getPackageDefinition().getId()).getNumberSequence();
            int purchaseNum = 0;
            // 对应的套餐定义，存在相同CODE组,但组中最大购买次数相同,则当前购买次数按组计算
            // if
            // (memberPackageDao.checkPackagePurchaseNumWithPackageCode(Long.valueOf(dto
            // .getDealerId()), dto.getActiveMemberId(), carId,
            // cmpi.getPackageDefinition()
            // .getId()) == 1) {
            // purchaseNum =
            // memberPackageDao.getPackagePurchaseNumWithPackageCode(Long
            // .valueOf(dto.getDealerId()), dto.getActiveMemberId(), carId, cmpi
            // .getPackageDefinition().getId());
            // } else {
            // // 对应的套餐定义，存在相同CODE组,但组中最大购买次数不同,则最大购买次数由卡片对应的套餐定义取得
            // purchaseNum = memberPackageDao.getPackagePurchaseNum(
            // Long.valueOf(dto.getDealerId()), dto.getActiveMemberId(), carId,
            // cmpi
            // .getPackageDefinition().getId());
            // }
            purchaseNum = memberPackageDao.getPackagesPurchaseNumWithSamePackageCode(UserUtil
                .getCurrentUser().getDealerId(), dto.getActiveMemberId(), carId, pd.getCode());

            if (dto.getPackageDefId() == null) {
                dto.setPackageDefId(cmpi.getPackageDefinition().getId());
            }
            // 超过
            if (maxPurchaseNum != 0 && maxPurchaseNum < purchaseNum+1  && !dto.isGiftPurchase()) {
                // 如果是礼品套餐则最大购买次数失效,最大购买次数为0时
                errorMsg = "卡号:" + dto.getPackageNumber() + "套餐:"
                    + packageDefinitionDao.findById(cmpi.getPackageDefinition().getId()).getName()
                    + ",已超过最大购买使用次数:" + maxPurchaseNum + ",不能激活！";
                throw new AppException(ERROR_CODE, errorMsg);
            }

//            List<PackageRedemptionLines> packageRedemptionLinesList = packageRedemptionLinesDao
//                .getPackageRedemptionLinesByDto(dto);
//            if (packageRedemptionLinesList != null && !packageRedemptionLinesList.isEmpty()) {
//                for (int i = 0; i < packageRedemptionLinesList.size(); i++) {
//                    // 集成套餐定义时新追加
//                    // 用ticket定义表来更新RedemptionLine表写入description,hasVerificationCode三个字段
//                    // description
//                    PackageRedemptionLines saveLine = packageRedemptionLinesDao
//                        .findById(packageRedemptionLinesList.get(i).getId());
//                    String description = iTicketLineDefineDao
//                        .getTicketDescriptionInfoByTicketId(packageRedemptionLinesList.get(i)
//                            .getTicket().getId());
//                    if (description != null) {
//                        saveLine.setDescription(description);
//                    }
//                    // hasVerificationCode
//                    if (packageRedemptionLinesList.get(i).getTicket().getVerificationMethod()
//                        .equals(CouponConstants.NEED_ACTIVICATION_YES)) {
//                        saveLine.setHasVerificationCode("Y");
//                    } else {
//                        saveLine.setHasVerificationCode("N");
//                    }
//                    // shortName
//                    String shortName = packageRedemptionLinesList.get(i).getTicket()
//                        .getTicketName();
//                    saveLine.setShortName(shortName);
//                    // 保存状态
//                    packageRedemptionLinesDao.update(saveLine);
//
//                    // 逐行添加package明细到MemberPackageRedemption(会员-卡-使用 表)
//                    PackageRedemptionLines lines = packageRedemptionLinesList.get(i);
//                    for (int j = 0; j < lines.getNumberOfUse(); j++) {
//                        MemberPackageRedemption memberPackageRedemption = new MemberPackageRedemption();
//                        memberPackageRedemption.setPackageId(packageId);
//                        memberPackageRedemption.setPackageRedemptionLine(lines);
//                        memberPackageRedemption.setShortName(shortName);
//                        // memberPackageRedemption.setShortName(lines.getShortName())
//                        // memberPackageRedemption.setShortName(lines.getShortName()+"第"+(j+1)+"次")
//                        if (description != null) {
//                            memberPackageRedemption.setDescription(description);
//                        }
//                        // 验证码
//                        memberPackageRedemption.setActivicationCode(String
//                            .valueOf((int) (100000 + Math.random() * 1000000)));
//                        // 0未使用，1已使用
//                        memberPackageRedemption.setStatus(0);
//
//                        MemberPackageRedemption mpr = memberPackageRedemptionDao
//                            .save(memberPackageRedemption);
//
//                        // MARS激活时会记录TicketItem信息使用是Item独立使用
//                        // 向tm_raw_package_redemption_lines_item插入数据
//
//                        List<TicketLine> itemList = iTicketLineDefineDao
//                            .getTicketLineInfoList(lines.getTicket().getId().toString());
//                        if(j==0){
//                            if (itemList != null && !itemList.isEmpty()) {
//                                for (TicketLine tl : itemList) {
//                                    PackageRedemptionLinesItem lineItem = new PackageRedemptionLinesItem();
//                                    lineItem.setTicket(packageRedemptionLinesList.get(i).getTicket());
//                                    lineItem.setMemberPackageInfo(memberPackageInfo);
//                                    lineItem.setMemberPackageRedemption(mpr);
//                                    lineItem.setItemName(tl.getItemName());
//                                    lineItem.setTicketLine(tl);
//                                    List<TicketPriceOfDealer> prices=ticketPriceOfDealerDao.getTicketPriceOfDealerByDealerIdAndTicketLineIdAndObjType(Long.parseLong(dto.getDealerId()), tl.getId(), "ticketLine");
//                                    if(prices!=null&&!prices.isEmpty()){
//                                        if(tl.getPrepayAmount()!=0.0){
//                                            Integer price=Double.valueOf(prices.get(0).getPrice()+tl.getPrepayAmount()/tl.getQuantity()).intValue();
//                                            lineItem.setPrice(price);
//                                        }
//                                    }else{
//                                        lineItem.setPrice(Double.valueOf(tl.getPrepayAmount()/tl.getQuantity()).intValue());
//                                    }
//                                    lineItem.setQuantity(tl.getQuantity());
//                                    packageRedemptionLinesItemDao.save(lineItem);
//                                }
//                            }
//                        }
//                    }
//                }
//            }

            MemberPackageInfoSubmitDto memberPackageInfoSubmitDto = new MemberPackageInfoSubmitDto();
            memberPackageInfoSubmitDto.setCarPlate(carPlate);
            memberPackageInfoSubmitDto.setMemberId(activeMemberId);
            memberPackageInfoSubmitDto.setDealerId(memberPackageInfo.getDealerId());
            memberPackageInfoSubmitDto.setPackageDefId(dto.getPackageDefId());
            memberPackageInfoSubmitDto.setPackageNumber(memberPackageInfo.getPackageNumber());
            memberPackageInfoSubmitDto.setPackageId(memberPackageInfo.getId());
            MemberPackageInfoSubmitDto memberPackageInfoSubmitDto1 = new MemberPackageInfoSubmitDto();
            memberPackageInfoSubmitDto1.setCarPlate(carPlate);
            memberPackageInfoSubmitDto1.setMemberId(activeMemberId);
            memberPackageInfoSubmitDto1.setDealerId(memberPackageInfo.getDealerId());
            memberPackageInfoSubmitDto1.setPackageDefId(dto.getPackageDefId());
            memberPackageInfoSubmitDto1.setPackageNumber(memberPackageInfo.getPackageNumber());
            if (!dto.isGiftPurchase()) {
                memberPackageInfo.setIsGift(0);
                memberGiftService.processTicketGift(memberPackageInfoSubmitDto);
                memberGiftService.processPackageGift(memberPackageInfoSubmitDto1);
            } else {
                // 设置卡片为礼物标致，使用页面显示时提示（赠送）使用
                memberPackageInfo.setIsGift(1);
            }
            // 更新PackageDefId
            PackageDefinition pd1 = packageDefinitionDao.findById(dto.getPackageDefId());
            memberPackageInfo.setPackageDefinition(pd1);
            // 更新 此卡 的 状态、激活是门店id、激活人id和 激活车牌id

            // 已生成0 已售出1 已激活2 已使用3 已用完4 已退款5
            memberPackageInfo.setStatus((byte) 2);

            // 购买门店id
            memberPackageInfo.setDealerId(memberPackageInfo.getDealerId());
            
            if(memberPackageInfo.getDealerId()!=UserUtil.getCurrentUser().getDealerId()){
                throw new AppException("saveMemberActivation", "该卡非本店销售,请到相应的购买门店："+memberPackageInfo.getDealerId()+",去激活!");
            }

            // 激活人id
            memberPackageInfo.setMemberId(activeMemberId);

            // 激活车牌id
            memberPackageInfo.setActiveCarId(carId);
            memberPackageInfo.setActiveMemberId(activeMemberId);
            memberPackageInfo.setActiveDealerId(Long.valueOf(dto.getDealerId()));
            // 激活日期
            memberPackageInfo.setActiveDate(calendar.getTime());
            // 价格
            memberPackageInfo.setActualPrice(memberPackageInfo.getActualPrice());
            // 返回Mars价格
            activationResultsDto.setPrice(memberPackageInfo.getActualPrice());
            // 失效日期
            if ("Y".equalsIgnoreCase(unitOfPeriod)) {
                calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + validPeriod);
            }
            if ("M".equalsIgnoreCase(unitOfPeriod)) {
                calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + validPeriod);
            }
            if ("D".equalsIgnoreCase(unitOfPeriod)) {
                calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + validPeriod);
            }

            // 过期时间
            memberPackageInfo.setExpireDate(calendar.getTime());

            // 设置Mars锁为状态0，CRM中该卡可以使用
            memberPackageInfo.setMarsLockStatus(0);

            // 写入soNumber
            memberPackageInfo.setSoNumberActive(dto.getSoNumberActive());

            // 更新表 会员-卡
            memberPackageDao.update(memberPackageInfo);

            // 更新 会员-车牌 关系表， 设置会员状态为已激活。
            CarPlateInfo cinfo = maintainCarPlateDao.findById(carId);

            if (cinfo != null) {
                cinfo.setMemberStatus("1");
                maintainCarPlateDao.update(cinfo);
            } else {
                errorMsg = "输入车牌有误，请检查。";
            }
            
            //@annson 判断虚拟卡，虚拟卡激活短信发送转移到结算时
            if (Utils.isNullOrEmpty(errorMsg) && !dto.isGiftPurchase()
            		//非虚拟卡 才发送短信
            		&& !(pd.getIsPhysicalCard() == PurchaseConstants.NON_FHYSICAL_CARD) 
            		) {
                // send SMS Message
                Map<String, String> paraMap = new HashMap<String, String>();
                MemberInfo memberInfo = memberDao.findById(activeMemberId);
                SmsContentDetailDto smsDto = new SmsContentDetailDto();
                if (memberInfo != null) {
                    paraMap.put(SmsTemplateVariable.VARIABLE_MEMBER_NAME,
                        memberInfo.getMemberName());
                    paraMap.put(SmsTemplateVariable.VARIABLE_PACKAGE_NAME, memberPackageInfo
                        .getPackageDefinition().getName());
                    paraMap.put(SmsTemplateVariable.VARIABLE_PACKAGE_NUM,
                        memberPackageInfo.getPackageNumber());
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    paraMap.put(SmsTemplateVariable.VARIABLE_EXPIRE_DATE,
                        sdf.format(memberPackageInfo.getExpireDate()));
                    
                    smsDto.setParaMap(paraMap);
                    smsDto.setDestMobile(Long.valueOf(memberInfo.getMobilePhone()));
                    smsDto
                        .setTemplateNo(SmsTemplateNoConstants.PACKAGE_ACTIVE_NOTIFICATION_TEMPLATE_NO);
                    smContentService.createSmsContent(smsDto);
                }
            }
        } else {
            if (memberPackageDao.checkPackageNumberAndActivationCodeByActivationDto(dto) == null) {
                errorMsg = "卡号:" + dto.getPackageNumber() + ",验证码有误，请重新输入。";
            } else {
                errorMsg = "卡号:" + dto.getPackageNumber() + ",已被激活，请重新输入。";
            }
        }

        if (Utils.notEmpty(errorMsg)) {
            LOGGER.error(errorMsg);
            throw new AppException("saveMemberActivationMars", errorMsg);
        }
        return activationResultsDto;
    }

    /**
     * {@inheritDoc} overridden: 赠送的优惠券提货券购买时专用方法
     * 
     * @Date : Sep 27, 2015
     * @see com.cap.bts.crmdp.dealer.activation.service.IActivationService#addPackageRedemptionListInfo(java.util.List)
     **/
    @Override
    public void addPackageRedemptionListInfoMars(
        List<PackageRedemptionLines> packageRedemptionLinesList, Long packageId)
        throws AppException {
        if (packageRedemptionLinesList != null && !packageRedemptionLinesList.isEmpty()) {
            for (int i = 0; i < packageRedemptionLinesList.size(); i++) {
                // 集成套餐定义时新追加
                // 用ticket定义表来更新RedemptionLine表写入description,hasVerificationCode三个字段
                // description
                PackageRedemptionLines saveLine = packageRedemptionLinesDao
                    .findById(packageRedemptionLinesList.get(i).getId());
                String description = iTicketLineDefineDao
                    .getTicketDescriptionInfoByTicketId(packageRedemptionLinesList.get(i)
                        .getTicket().getId());
                if (description != null) {
                    saveLine.setDescription(description);
                }
                // hasVerificationCode
                if (packageRedemptionLinesList.get(i).getTicket().getVerificationMethod()
                    .equals(CouponConstants.NEED_ACTIVICATION_YES)) {
                    saveLine.setHasVerificationCode("Y");
                } else {
                    saveLine.setHasVerificationCode("N");
                }
                // shortName
                String shortName = packageRedemptionLinesList.get(i).getTicket().getTicketName();
                saveLine.setShortName(shortName);
                // 保存状态
                packageRedemptionLinesDao.update(saveLine);

                // 逐行添加package明细到MemberPackageRedemption(会员-卡-使用 表)
                PackageRedemptionLines lines = packageRedemptionLinesList.get(i);
                for (int j = 0; j < lines.getNumberOfUse(); j++) {
                    MemberPackageRedemption memberPackageRedemption = new MemberPackageRedemption();
                    memberPackageRedemption.setPackageId(packageId);
                    memberPackageRedemption.setPackageRedemptionLine(lines);
                    memberPackageRedemption.setShortName(shortName + "(赠送)");
                    // memberPackageRedemption.setShortName(lines.getShortName())
                    if (description != null) {
                        memberPackageRedemption.setDescription(description);
                    }
                    // 验证码
                    memberPackageRedemption.setActivicationCode(String.valueOf((int) (100000 + Math
                        .random() * 1000000)));
                    // 0未使用，1已使用
                    memberPackageRedemption.setStatus(0);

                    memberPackageRedemptionDao.save(memberPackageRedemption);
                }
            }
        }

    }

    /**
     * {@inheritDoc} overridden:
     * 
     * @Date : Nov 4, 2015
     * @see com.cap.bts.crmdp.dealer.activation.service.IActivationService#packageListActivationMars(java.util.List,
     *      java.lang.Long)
     **/
    @Override
    public Map<String, Object> packageListActivationMars(MemberPackageInfoSubmitDto dto)
        throws AppException {
        Map<String, Object> rtnMap = new HashMap<String, Object>();
        List<CardDto> dtoList = dto.getCardList();
        List<CardDto> cardList = new ArrayList<CardDto>();

        User user = new User();
        user.setId(0L);
        user.setDealerId(dto.getDealerId());
        UserUtil.setMarsCurrentUser(user);

        WSCrmActivationResultDto wsDto = new WSCrmActivationResultDto();
        if (dtoList != null && !dtoList.isEmpty()) {
            for (CardDto cardDto : dtoList) {
                if (cardDto.getActiveCode() != null && !"".equals(cardDto.getActiveCode())) {
                    dto.setPackageNumber(cardDto.getCardNumber());
                    dto.setPackageActiveCode(cardDto.getActiveCode());
                    try {
                        dto.setDealerId(dto.getDealerId());
                        List<PackageDefinition> list = memberGiftService
                            .getPackageGiftListInfo(dto);
                        StringBuilder successMsg = new StringBuilder();
                        if (list != null && !list.isEmpty()) {
                            successMsg.append("卡号:" + dto.getPackageNumber() + ",套餐:");
                            for (Iterator<PackageDefinition> iterator = list.iterator(); iterator
                                .hasNext();) {
                                PackageDefinition packageDefinition = (PackageDefinition) iterator
                                    .next();
                                successMsg.append(packageDefinition.getName() + ",");
                            }
                        }
                        List<Ticket> listTicket = memberGiftService.getTicketGiftListInfo(dto);
                        if (listTicket != null && !listTicket.isEmpty()) {
                            for (Iterator<Ticket> iterator = listTicket.iterator(); iterator
                                .hasNext();) {
                                Ticket t = (Ticket) iterator.next();
                                if ("COUPON".equals(t.getTicketType())) {
                                    successMsg.append("卡号:" + dto.getPackageNumber() + ",优惠券:");
                                } else {
                                    successMsg.append("卡号:" + dto.getPackageNumber() + ",提货券:");
                                }
                                successMsg.append(t.getTicketName() + ",");
                            }
                        }
                        if (!successMsg.toString().isEmpty()) {
                            successMsg.append("已赠送！");
                            rtnMap.put(SUCCESS_MSG, successMsg.toString());
                            rtnMap.put(RESULT, true);
                            // 记入Mars
                            rtnMap.put(SUCCESS_MSG, SUCCESS);
                        } else {
                            rtnMap.put(SUCCESS_MSG, SUCCESS);
                            rtnMap.put(RESULT, true);
                            // 记入Mars
                            rtnMap.put(SUCCESS_MSG, SUCCESS);
                        }
                        ActivationResultsDto resultsDto = this
                            .saveMemberPurchaseAndActivationNowMars(dto);
                        cardDto.setAmount(String.valueOf(resultsDto.getPrice()));
                        
                        
                        if ((Boolean) rtnMap.get(RESULT)) {
                        	cardList.add(cardDto);
                        }
                        
                       /* if ((Boolean) rtnMap.get(RESULT)) {
                            wsDto.setCards(cardList);
                            wsDto.setStatusCode("1");

                            wsDto.setVehicleNo(dto.getMarsCarId());
                            wsDto.setPreVehicleNo(dto.getMarsCarId());
                            wsDto.setVehicleStatus("已绑定套餐");
                            wsDto.setCustNo(dto.getMarsCustomerId());
                            wsDto.setPreCustNo(dto.getMarsCustomerId());
                            wsDto.setCustStatus("会员");
                            wsDto.setServerCenter(String.valueOf(dto.getDealerId()));
                        } else {
                            wsDto.setStatusCode("0");
                        }
                        wsDto.setSoNumber(dto.getSoNumberActive());

                        CRM2MARS marsService = new CRM2MARS();
                        MarsResultDto resultDto = marsService.activation(wsDto);

                        if ("0".equals(resultDto.getStatusCode())) {
                            LOGGER.error("Mars接口错误" + resultDto.getResultMessage());
                            rtnMap.put("errorMsg", resultDto.getResultMessage());
                            throw new AppException("mars", resultDto.getResultMessage());
                        }*/

                    } catch (Exception e) {
                        LOGGER.error("saveMemberActivation", e);
                        throw new AppException("mars", e.getMessage());
                    }
                }
            }
            
            //完成激活，再调用MARS 接口
            if (!cardList.isEmpty()) {
                wsDto.setCards(cardList);
                wsDto.setStatusCode("1");

                wsDto.setVehicleNo(dto.getMarsCarId());
                wsDto.setPreVehicleNo(dto.getMarsCarId());
                wsDto.setVehicleStatus("已绑定套餐");
                wsDto.setCustNo(dto.getMarsCustomerId());
                wsDto.setPreCustNo(dto.getMarsCustomerId());
                wsDto.setCustStatus("会员");
                wsDto.setServerCenter(String.valueOf(dto.getDealerId()));
                
                
                wsDto.setSoNumber(dto.getSoNumberActive());

                CRM2MARS marsService = new CRM2MARS();
                MarsResultDto resultDto = marsService.activation(wsDto);

                if ("0".equals(resultDto.getStatusCode())) {
                    LOGGER.error("Mars接口错误" + resultDto.getResultMessage());
                    rtnMap.put("errorMsg", resultDto.getResultMessage());
                    throw new AppException("mars", resultDto.getResultMessage());
                }
                
            } 
            
        }
        return rtnMap;
    }

    /**
     * {@inheritDoc} overridden:
     * 
     * @Date : Nov 4, 2015
     * @see com.cap.bts.crmdp.dealer.activation.service.IActivationService#packageListActivationMars(java.util.List,
     *      java.lang.Long)
     **/
    @Override
    public Map<String, Object> packageActivationMars(MemberPackageInfoSubmitDto dto)
        throws AppException {
        Map<String, Object> rtnMap = new HashMap<String, Object>();
        CardDto card = new CardDto();
        WSCrmActivationResultDto wsDto = new WSCrmActivationResultDto();

        try {
            List<PackageDefinition> list = memberGiftService.getPackageGiftListInfo(dto);
            StringBuilder successMsg = new StringBuilder();
            if (list != null && !list.isEmpty()) {
                successMsg.append("卡号:" + dto.getPackageNumber() + ",套餐:");
                for (Iterator<PackageDefinition> iterator = list.iterator(); iterator.hasNext();) {
                    PackageDefinition packageDefinition = (PackageDefinition) iterator.next();
                    successMsg.append(packageDefinition.getName() + ",");
                }
            }
            List<Ticket> listTicket = memberGiftService.getTicketGiftListInfo(dto);
            if (listTicket != null && !listTicket.isEmpty()) {
                for (Iterator<Ticket> iterator = listTicket.iterator(); iterator.hasNext();) {
                    Ticket t = (Ticket) iterator.next();
                    if ("COUPON".equals(t.getTicketType())) {
                        successMsg.append("卡号:" + dto.getPackageNumber() + ",优惠券:");
                    } else {
                        successMsg.append("卡号:" + dto.getPackageNumber() + ",提货券:");
                    }
                    successMsg.append(t.getTicketName() + ",");
                }
            }
            if (!successMsg.toString().isEmpty()) {
                successMsg.append("已赠送！");
                rtnMap.put(SUCCESS_MSG, successMsg.toString());
                rtnMap.put(RESULT, true);
                // 记入Mars
                wsDto.setResultMessage(SUCCESS);
            } else {
                rtnMap.put(SUCCESS_MSG, SUCCESS);
                rtnMap.put(RESULT, true);
                // 记入Mars
                wsDto.setResultMessage(SUCCESS);
            }

            ActivationResultsDto resultsDto = this.saveMemberPurchaseAndActivationNowMars(dto);

            card.setCardNumber(dto.getPackageNumber());
            card.setAmount(String.valueOf(resultsDto.getPrice()));

            MemberPackageInfo mpi = memberPackageDao.getMemeberPackageInfoByPackageNumber(dto
                .getPackageNumber());
            CarPlateInfo preCarPlateInfo = null;
            MemberInfo preMemberInfo = null;
            if (mpi != null) {
                preCarPlateInfo = maintainCarPlateDao.findById(mpi.getCarId());
                preMemberInfo = memberDao.findById(mpi.getMemberId());
            }

            List<CardDto> cards = new ArrayList<CardDto>();
            CRM2MARS marsService = new CRM2MARS();
            if ((Boolean) rtnMap.get(RESULT)) {
//                if (card.getActiveCode() != null && !"".equals(card.getActiveCode())) {
//                    cards.add(card);
//                }
            	
                cards.add(card);
                
                wsDto.setCards(cards);
                wsDto.setVehicleNo(dto.getMarsCarId());
                if (preCarPlateInfo != null) {
                    wsDto.setPreVehicleNo((String) preCarPlateInfo.getMarsCarId());
                }
                wsDto.setVehicleStatus("已绑定套餐");
                wsDto.setCustNo(dto.getMarsCustomerId());
                if (preMemberInfo != null) {
                    wsDto.setPreCustNo((String) preMemberInfo.getMarsMemberId());
                }
                wsDto.setServerCenter(String.valueOf(dto.getDealerId()));
                wsDto.setCustStatus("会员");
                wsDto.setStatusCode("1");
                wsDto.setSoNumber(dto.getSoNumberActive());
                MarsResultDto resultDto = marsService.activation(wsDto);
                if ("0".equals(resultDto.getStatusCode())) {
                    LOGGER.error("Mars interface error" + resultDto.getResultMessage());
                    UserUtil.removeMarsCurrentUser();
                    throw new AppException(PACKAGE_ACTIVATION_MARS, resultDto.getResultMessage());
                }
            }

        } catch (AppException e) {
            LOGGER.error(PACKAGE_ACTIVATION_MARS, e);
            throw new AppException(PACKAGE_ACTIVATION_MARS, e.getMessage());
        }

        return rtnMap;
    }

    /**
     * {@inheritDoc} overridden:
     * 
     * @Date : Oct 9, 2015
     * @see com.cap.bts.crmdp.dealer.activation.service.IActivationService#checkPackageNumberByActivationDto(com.cap.bts.crmdp.dealer.activation.dto.ActivationQueryDto)
     **/
    @Override
    public MemberPackageInfo checkPackageNumberByActivationDto(ActivationQueryDto activationQueryDto)
        throws AppException {
        return memberPackageDao.checkPackageNumberByActivationDto(activationQueryDto);
    }
}
